<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * Class Admin 管理员模块
 */
class Login extends CI_Controller {
    protected $assets;
    public function __construct()
    {
        parent::__construct();
        // 鉴权
        $this->load->library('lw_auth');
        // 专用的，上面要么用不上，要么全局的
        $this->assets = $this->lw_assets->getAssets(array('backstretch','validation'));
        // 实例化数据库
        $this->load->library('lw_db',array('tb_name'=>'tb_admin'),'tb_admin');
        $this->load->library('lw_db',array('tb_name'=>'tb_role'),'tb_role');
    }

    /** 初期构想三种情况：1默认登录情况，2登录失效重新登录情况，3注销登录情况（内容沿用1，但是要注销session）
     * @param int $type
     */
    public function index($type=1)
    {
        $info = $this->input->post();
        if(isset($info['doSubmit'])){
            $rowAdmin = $this->tb_admin->get_one(array('loginname'=>$info['loginname'],'password'=>md5($info['password'])));
            if(!$rowAdmin){
                echo $this->lw_load->jsonResult(false,array('msg'=>'登录名或密码错误！'));
                exit;
            }
            if(!$rowAdmin['enabled']==1){
                echo $this->lw_load->jsonResult(false,array('msg'=>'当前管理员账号已失效，请联系管理员！'));
                exit;
            }
            $rowRole = $this->tb_role->get_one(array('id'=>$rowAdmin['role_id']));
            // 存 session 数据
            $this->session->set_userdata('adminId',$rowAdmin['id']);
            $this->session->set_userdata('username',$rowAdmin['username']);
            $this->session->set_userdata('telephone',$rowAdmin['telephone']);
            $this->session->set_userdata('roleId',$rowAdmin['role_id']);
            $this->session->set_userdata('topRole',($rowRole['parent_id']==0)?true:false);
            echo $this->lw_load->jsonResult(true,array('msg'=>'登录成功正在跳转..'));
        }else{
            $this->assets['pageCss'] = array(base_url('assets/pages/login/login.css'));
            switch($type){
                case 1:
                    $this->assets['pageJs'] = array(base_url('assets/pages/login/login.js'));
                    break;
                case 2:
                    $data['backUrl'] = $this->session->userdata('backUrl');
                    $this->assets['pageJs'] = array(base_url('assets/pages/login/relogin.js'));
                    break;
                case 3:
                    $this->assets['pageJs'] = array(base_url('assets/pages/login/login.js'));
                    $this->session->sess_destroy();
                    break;
                define:
                    $this->assets['pageJs'] = array(base_url('assets/pages/login/login.js'));
                    break;
            }
            $data['assets'] = $this->assets;
            $this->lw_load->loginView($data);
        }
    }

    public function reloginAjax()
    {
        $info = $this->input->post();
        // 判断是否为提交按钮
        if(isset($info['submit'])){
            // 提交表单数据
            unset($info['submit']);
            $info['password'] = md5($info['password']);
            $rowAdmin = $this->tb_admin->get_one($info);
            if(!$rowAdmin){
                echo $this->lw_load->jsonResult(false,array('msg'=>'登录名或密码错误！'));
                exit;
            }
            if(!$rowAdmin['enabled']==1){
                echo $this->lw_load->jsonResult(false,array('msg'=>'当前管理员账号已失效，请联系管理员！'));
                exit;
            }
            $rowRole = $this->tb_role->get_one(array('id'=>$rowAdmin['role_id']));
            // 存 session 数据
            $this->session->set_userdata('adminId',$rowAdmin['id']);
            $this->session->set_userdata('username',$rowAdmin['username']);
            $this->session->set_userdata('telephone',$rowAdmin['telephone']);
            $this->session->set_userdata('roleId',$rowAdmin['role_id']);
            $this->session->set_userdata('topRole',($rowRole['parent_id']==0)?true:false);
            echo $this->lw_load->jsonResult(true,array('msg'=>'登录成功'));
        } else {
            // 请求视图
            $html = $this->load->view('system/login/modal_login',null,true);
            echo $this->lw_load->jsonResult(true,array('msg'=>'重新登录','html'=>$html));
        }
    }


}